<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/3/21
 * Time: 9:33
 */

namespace App\Http\Controllers;


use App\Comment;
use App\Film;
use Illuminate\Support\Facades\Session;

class IndexController extends Controller
{
    function index(){
        $screening=Film::orderby('date','desc')->having('screening','=',1)->get();
        $num=count($screening);
        $num=ceil($num/4);
        $film=Film::orderby('comment_num','desc')->get();
        $i=1;
        $rank=Film::all();
        $rank=$rank->sortByDesc('score')->take(20);
        $review=Comment::orderby('assist_num','desc')->limit(4)->get();
        //通过评论中的影片id查询影片相关信息
        $info=array();
        $n=0;
        foreach ($review as $item){
           $info[$n]=Film::find($item['film_id']);
           $item['title']=$info[$n]['title'];
           $item['poster']=$info[$n]['poster'];
            $n++;
        }
        return view('index',['film'=>$film,'screening'=>$screening,'num'=>$num,'i'=>$i,'rank'=>$rank,'review'=>$review]);
    }

    function tag(){
        if(isset($_POST['tag'])){
            $tag=$_POST['tag'];
            if($tag=='热门'){
                $data=Film::orderby('comment_num','desc')->get();
                $data=json_encode($data);
                return $data;
            }
            elseif($tag=='最新'){
                $data=Film::orderby('date','desc')->get();
                $data=json_encode($data);
                return $data;
            }
            elseif($tag=='经典'){
                $data=Film::orderby('date','asc')->get();
                $data=json_encode($data);
                return $data;
            }
            else{
            $data=Film::where('type','like','%'.$tag.'%')->get();
            $data=json_encode($data);
            return $data;
            }
        }
        else{
            return "no";
        }
    }

    function test(){
        return view('layout');
    }
}